home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Tool Chest / Testing & Debugging / Virtual User / Virtual User Current Release / Examples / Example External Tools / ProcessTool / MiscServices.cp < prev    next >
Encoding:
Text File  |  1998-06-04  |  7.3 KB  |  282 lines  |  [TEXT/MPS ]

  1. /*
  2.  *    File:        MiscServices.cp
  3.  *
  4.  *    Contains:    xxx put contents here xxx
  5.  *
  6.  *    Written by:    Rick Violet
  7.  *
  8.  *    Copyright:    © 1992-1994 by Apple Computer, Inc., all rights reserved.
  9.  *
  10.  *    Change History (most recent first):
  11.  *
  12.  *         <2>     1/27/94    BD        String utilities moved to TextUtils.h from Packages.h.
  13.  *        <1+>     1/27/94    BD        Relstring moved to TextUtils.h from Packages.h.
  14.  *        <1+>     4/14/93    RV        
  15.  *        <5+>    11/19/92    RV        
  16.  *                11/18/92    RV        xxx put comment here xxx
  17.  *
  18.  *    To Do:
  19.  */
  20.  
  21. #ifndef        __MiscServices__
  22. #include        "MiscServices.h"
  23. #endif
  24.  
  25. #ifndef        __Application__
  26. #include        "Application.h"
  27. #endif
  28.  
  29. #ifndef        __TEXTUTILS__
  30. #include        <TextUtils.h>
  31. #endif
  32.  
  33. //—————————————————————————————————————————————————————————————————————————————————————
  34. //    AddService::AddService    -    constructor.
  35. //—————————————————————————————————————————————————————————————————————————————————————
  36. AddService::AddService():Service( "Add" )
  37. {
  38. }
  39.  
  40. //—————————————————————————————————————————————————————————————————————————————————————
  41. //    AddService::~AddService    -    destructor.
  42. //—————————————————————————————————————————————————————————————————————————————————————
  43. AddService::~AddService()
  44. {
  45. }
  46.  
  47. //—————————————————————————————————————————————————————————————————————————————————————
  48. //    AddService::ProcessRequest    -    implement Addition of V.U. Numbers
  49. //—————————————————————————————————————————————————————————————————————————————————————
  50. OSErr
  51. AddService::ProcessRequest( Request* pReq )
  52. {
  53.     OSErr            tErr = noErr;
  54.     ScriptValue*    tNum1;
  55.     ScriptValue*    tNum2;
  56.     short            tResult;
  57.     ValueKind        tVKind;
  58.     
  59.         //————    Get the first parameter
  60.     tVKind = kVUNumberKind;
  61.     tErr = pReq->GetNthParam( 1, tNum1, tVKind );
  62.  
  63.         //————    Did we get a valid ScriptValue object pointer?
  64.     if( tErr || (tNum1 == nil))
  65.     {
  66.             //————    No, bail out
  67.         pReq->SetErrorCode( paramErr );
  68.         pReq->SetErrorMessage( "Failed to extract parameter 1" );
  69.         return paramErr;
  70.     }
  71.     
  72.         //————    Get the second parameter
  73.     tVKind = kVUNumberKind;
  74.     tErr = pReq->GetNthParam( 2, tNum2, tVKind );
  75.  
  76.         //————    Did we get a valid ScriptValue object pointer?
  77.     if( tErr || (tNum1 == nil))
  78.     {
  79.             //————    No, bail out
  80.         pReq->SetErrorCode( paramErr );
  81.         pReq->SetErrorMessage( "Failed to extract parameter 2" );
  82.         return paramErr;
  83.     }
  84.     
  85.         //————    The parameters are of the correct type
  86.         //————    add them and return the result to V.U.
  87.     tResult =   ((VUNumber*)tNum1)->GetNumber();
  88.     tResult +=  ((VUNumber*)tNum2)->GetNumber();
  89.     
  90.     pReq->SetReturnValue( tResult );
  91.     return noErr;
  92. }
  93.  
  94. //—————————————————————————————————————————————————————————————————————————————————————
  95. //    EchoService::EchoService    -    constructor.
  96. //—————————————————————————————————————————————————————————————————————————————————————
  97. EchoService::EchoService():Service( "Echo")
  98. {
  99. }
  100.  
  101. //—————————————————————————————————————————————————————————————————————————————————————
  102. //    EchoService::~EchoService    -    destructor.
  103. //—————————————————————————————————————————————————————————————————————————————————————
  104. EchoService::~EchoService()
  105. {
  106. }
  107.  
  108. //—————————————————————————————————————————————————————————————————————————————————————
  109. //    EchoService::ProcessRequest    -    echo the parameters back
  110. //—————————————————————————————————————————————————————————————————————————————————————
  111. OSErr
  112. EchoService::ProcessRequest( Request* pReq )
  113. {
  114.     OSErr            tErr = noErr;
  115.     ScriptValue*    tVal;
  116.     VUList*            tReturnList;
  117.     short            tParamCount;
  118.     short            i;
  119.     ValueKind        tVKind;
  120.     
  121.     tParamCount = pReq->GetParamCount();
  122.     
  123.     if( tParamCount == 1 )
  124.     {
  125.         tVKind = kVUAnyKind;
  126.         tErr = pReq->GetNthParam( 1, tVal, tVKind );
  127.         if( tVal && !tErr )
  128.         {
  129.             pReq->SetReturnValue( tVal->Clone() );
  130.         }
  131.         else
  132.         {
  133.             tErr = memFullErr;
  134.         }
  135.     }
  136.     else
  137.     {
  138.         tReturnList = new VUList();
  139.         if( tReturnList )
  140.         {
  141.             for( i = 1; i <= tParamCount; i++ )
  142.             {
  143.                 tVKind = kVUAnyKind;
  144.                 tErr = pReq->GetNthParam( i, tVal, tVKind );
  145.                 if( tVal && (tErr == noErr) )
  146.                 {
  147.                     tReturnList->PutNthItem(  tVal->Clone(), i );
  148.                 }
  149.             }
  150.             pReq->SetReturnValue( tReturnList );
  151.         }
  152.     }
  153.     return tErr;
  154. }
  155.  
  156. //—————————————————————————————————————————————————————————————————————————————————————
  157. //    ErrorService::ErrorService    -    constructor.
  158. //—————————————————————————————————————————————————————————————————————————————————————
  159. ErrorService::ErrorService():Service( "Error")
  160. {
  161. }
  162.  
  163. //—————————————————————————————————————————————————————————————————————————————————————
  164. //    ErrorService::~ErrorService    -    destructor.
  165. //—————————————————————————————————————————————————————————————————————————————————————
  166. ErrorService::~ErrorService()
  167. {
  168. }
  169.  
  170. //—————————————————————————————————————————————————————————————————————————————————————
  171. //    ErrorService::ProcessRequest    -    Error the parameters back
  172. //—————————————————————————————————————————————————————————————————————————————————————
  173. OSErr
  174. ErrorService::ProcessRequest( Request* pReq )
  175. {
  176.     OSErr            tErr = noErr;
  177.     ScriptValue*    tNum1;
  178.     ScriptValue*    tNum2;
  179.     short            tErrCode;
  180.     char*            tErrString;
  181.     ValueKind        tVKind;
  182.     
  183.         //————    Get the first parameter, the code to return
  184.     tVKind = kVUNumberKind;
  185.     tErr = pReq->GetNthParam( 1, tNum1, tVKind );
  186.  
  187.         //————    Did we get a valid ScriptValue object pointer?
  188.     if( tErr || (tNum1 == nil))
  189.     {
  190.             //————    No, bail out
  191.         pReq->SetErrorCode( paramErr );
  192.         pReq->SetErrorMessage( "Failed to extract parameter 1" );
  193.         return paramErr;
  194.     }
  195.     else
  196.     {
  197.         tErrCode = ((VUNumber*)tNum1)->GetNumber();
  198.         pReq->SetErrorCode( tErrCode );
  199.     }
  200.  
  201.         //————    Get the second parameter, the string to return
  202.     tVKind = kVUStringKind;
  203.     tErr = pReq->GetNthParam( 2, tNum2, tVKind );
  204.  
  205.         //————    Did we get a valid ScriptValue object pointer?
  206.     if( tErr || (tNum2 == nil))
  207.     {
  208.             //————    No, bail out
  209.         pReq->SetErrorCode( paramErr );
  210.         pReq->SetErrorMessage( "Failed to extract parameter 2" );
  211.         return paramErr;
  212.     }
  213.     else
  214.     {
  215.         tErrString = ((VUString*)tNum2)->GetText();
  216.         pReq->SetErrorMessage( tErrString );
  217.     }
  218.     return noErr;
  219. }
  220.  
  221. //—————————————————————————————————————————————————————————————————————————————————————
  222. //    WaitService::WaitService    -    constructor.
  223. //—————————————————————————————————————————————————————————————————————————————————————
  224. WaitService::WaitService():Service( "Wait" )
  225. {
  226. }
  227.  
  228. //—————————————————————————————————————————————————————————————————————————————————————
  229. //    WaitService::~WaitService    -    destructor.
  230. //—————————————————————————————————————————————————————————————————————————————————————
  231. WaitService::~WaitService()
  232. {
  233. }
  234.  
  235. //—————————————————————————————————————————————————————————————————————————————————————
  236. //    WaitService::ProcessRequest    -    Wait the parameters back
  237. //—————————————————————————————————————————————————————————————————————————————————————
  238. OSErr
  239. WaitService::ProcessRequest( Request* pReq )
  240. {
  241.     unsigned long    tStopTicks;
  242.     short            tWaitInterval;
  243.     ScriptValue*    tParam;
  244.     Boolean            tCanceled = false;
  245.     ValueKind        tVKind;
  246.     OSErr            tErr;
  247.     
  248.     
  249.         //————    Get the first parameter
  250.     tVKind = kVUNumberKind;
  251.     tErr = pReq->GetNthParam( 1, tParam, tVKind );
  252.  
  253.         //————    Did we get a valid ScriptValue object pointer?
  254.     if( tParam == nil || tErr )
  255.     {
  256.             //————    No, bail out
  257.         pReq->SetErrorCode( paramErr );
  258.         pReq->SetErrorMessage( "Failed to extract parameter 1" );
  259.         return paramErr;
  260.     }
  261.     
  262.     tWaitInterval = ((VUNumber*)tParam)->GetNumber() * 60; //————    Convert seconds to Ticks
  263.         
  264.     tStopTicks = TickCount() + tWaitInterval;
  265.     while( (TickCount() < tStopTicks) && (!tCanceled) )
  266.     {
  267.         if( CheckForCancel( pReq ) )
  268.         {
  269.             tCanceled = true;
  270.         }
  271.     }
  272.     
  273.     if( tCanceled )
  274.     {
  275.         pReq->SetReturnValue( "Canceled" );
  276.     }
  277.     else
  278.     {
  279.         pReq->SetReturnValue( "Not Canceled" );
  280.     }
  281. }
  282.